<?
  class LabelController extends CFD_Controller_Action
  {
    public function getimageAction()
	{
	   $this->_helper->layout->disableLayout();
	   $vehObj = new CFD_CAP_Vehicles();

	   header('Content-Type: image/jpeg');
	   echo $file_data = $vehObj->vehicleImage($_GET['capid']);

	   if ($file_data)
		file_put_contents(CAR_IMG_CAPID_DIRECTORY.$_GET['capid'].".jpg", $file_data);

	   exit;
	}

    public function tpl1Action()
	{
	   $this->_helper->layout->disableLayout();

	   // fetch white label
	   $labelHash = $this->getRequest()->getParam('label');
	   $wlTable = new Application_Model_WhiteLabels();
	   $wlRow = $wlTable->findByHash($labelHash);

	   $vrmSession = new Zend_Session_Namespace('vrmLookup');
	   $vehObj = new CFD_CAP_Vehicles();
	   $reg = $_POST['reg'];

	   if ($_POST['reg'])
	   {
	     if ($vrmSession->vrmLookup['LOOKUP_VRM'] == $_POST['reg'])
	     {
	       $this->view->vehicle = $vrmSession->vrmLookup;
	     }
	     else
	     {
	       $vrmSession->vrmLookup = $this->view->vehicle = $vehObj->DVLALookupVRM($reg);
	     }
	   }

	   // display car details 
	   if ($_POST['step'] == 2)
	   {
		 if ($this->view->vehicle['DATA'])
		   $this->_helper->viewRenderer->setRender('tpl1-step2'); 
		 else
		   $this->_helper->viewRenderer->setRender('car-not-found'); 

		 return 0;
	   }

	   if ($_POST['step'] == 3)
	   {
		 $this->_helper->viewRenderer->setRender('tpl1-step3'); 

		 return 0;
	   }

	   if ($_POST['step'] == 4)
	   {
		 $this->view->prev_data = base64_encode(serialize($_POST));

		 $this->_helper->viewRenderer->setRender('tpl1-step4'); 

		 return 0;
	   }

	   if ($_POST['step'] == 5)
	   {
		 if ($_FILES['photo1'] && strpos(strtolower($_FILES['photo1']['type']), 'image') === 0)
		 {
		    while (true)
			{
			  $file_name = rand(0, getrandmax())."_".$_FILES['photo1']['name'];
			  $path = CAR_IMG_DIRECTORY.$file_name;

			  if (file_exists($path))
			    continue;

    		  move_uploaded_file($_FILES["photo1"]["tmp_name"], $path);
			  break;
			}
		 }

		 $dataCombined = unserialize(base64_decode($_POST['prev_data']));
		 $dataCombined['file_name1'] = $file_name;
		 $dataCombined['major_selling_points'] = $_POST['major_selling_points'];
		 $dataCombined['spec'] = $_POST['spec'];

		 $this->view->prev_data  = base64_encode(serialize($dataCombined));

		 $this->_helper->viewRenderer->setRender('tpl1-step5'); 

		 return 0;
	   }

	   if ($_POST['step'] == 6)
	   {
		 $dataCombined = unserialize(base64_decode($_POST['prev_data']));
		 $data = $_POST;

		 $dataCombined['mechanical_condition'] = $data['mechanical_condition'];
		 $dataCombined['interior_condition'] = $data['interior_condition'];

		 unset($data['step']);
		 unset($data['prev_data']);
		 unset($data['reg']);
		 unset($data['mechanical_condition']);
		 unset($data['interior_condition']);
		 unset($data['condition']);

		 foreach ($data as $key => $condition)
		   $dataCombined['conditions'][ $key ] = $condition;

		 $this->view->prev_data  = base64_encode(serialize($dataCombined));

		 $this->_helper->viewRenderer->setRender('tpl1-step6'); 
		 return;
	   }

	   if ($_POST['step'] == 7)
	   {
		 $this->view->prev_data  = $_POST['prev_data'];

		 $this->_helper->viewRenderer->setRender('tpl1-step7'); 
	   }

	   
	   if ($_POST['step'] == 8)
	   {
		 $dataCombined = unserialize(base64_decode($_POST['prev_data']));

		 $data = $_POST;
		 unset($data['step']);
		 unset($data['reg']);
		 unset($data['prev_data']);

		 $dataCombined['client'] = $data;
		 $this->view->prev_data  = base64_encode(serialize($dataCombined));


		 // price modification !!!
		  
		   // check conditions
	     $cpmTable = new Application_Model_CarPriceModifiers();
	     $conditions = $cpmTable->fetchArray();

		 foreach ($conditions as $k => $v)
		   $conditions[ str_replace(" ", "_", $k) ] = $v;

		 $priceModiciation = 0;
		 foreach ((array)$dataCombined['conditions'] as $c_name => $cond)
		   $priceModiciation -= $conditions[$c_name][$cond];

		  // other price reductions

		  if ($dataCombined['left_hand_drive'] == "Yes")
		    $priceModiciation -= 1000;

		 if ($dataCombined['spec']['tinted_windows'] == 'on')
		    $priceModiciation -= 170;

		 $vehicleCostObj = $vehObj->getUsedValuesForIDYearMonthMileage($this->view->vehicle['DATA']['CAP']['CAPID'], $this->view->vehicle['DATA']['CAP']['PLATE_YEAR'], $this->view->vehicle['DATA']['CAP']['PLATE_MONTH'], $dataCombined['mileage']);

	     $valuationsTable = new Application_Model_CarValuations();

		 $valRow = $valuationsTable->createRow();

         $valRow->reg = $dataCombined['reg'];
         $valRow->white_labels_id = $wlRow->id;

         $valRow->mileage = $dataCombined['mileage'];
         $valRow->prev_owners = $dataCombined['prev_owners'];
         $valRow->finance_outstanding = $dataCombined['finance_outstanding'];
         $valRow->service_history = $dataCombined['service_history'];
         $valRow->is_scrap = $dataCombined['is_scrap'];
         $valRow->left_hand_drive = $dataCombined['left_hand_drive'];
         $valRow->file_name1 = $dataCombined['file_name1'];
         $valRow->major_selling_points = $dataCombined['major_selling_points'];
         $valRow->mechanical_condition = $dataCombined['mechanical_condition'];
         $valRow->interior_condition = $dataCombined['interior_condition'];
         $valRow->conditions_ser = serialize($dataCombined['conditions']);
		 foreach ($dataCombined['spec'] as $spec_name => $spec_value)
           $valRow->{'spec_'.$spec_name} = $spec_value;

         $valRow->client_name = $dataCombined['client']['name'];
         $valRow->client_email = $dataCombined['client']['email'];
         $valRow->client_postcode = $dataCombined['client']['postcode'];
         $valRow->client_phone = $dataCombined['client']['phone'];

         $valRow->CAPID = $this->view->vehicle['DATA']['CAP']['CAPID'];
         $valRow->cost_retail = $vehicleCostObj->Retail;
         $valRow->cost_clean = $vehicleCostObj->Clean;
         $valRow->cost_average = $vehicleCostObj->Average;
         $valRow->cost_error = $vehicleCostObj->FailMessage;
         $valRow->cost_reduction = $priceModiciation;

		 $valRow->manufacturer = $this->view->vehicle['DATA']['CAP']['MANUFACTURER'];
		 $valRow->model = $this->view->vehicle['DATA']['CAP']['MODEL'];
		 $valRow->plate = $this->view->vehicle['DATA']['CAP']['PLATE'];
		 $valRow->derivative = $this->view->vehicle['DATA']['CAP']['DERIVATIVE'];
		 $valRow->transmission = $this->view->vehicle['DATA']['CAP']['TRANSMISSION'];
		 $valRow->fueltype = $this->view->vehicle['DATA']['CAP']['FUELTYPE'];
		 $valRow->cost_decrease_prc = $conditions['decrease_prices_prc']['decrease_prices_prc'];
		 $valRow->owner_users_id = $this->user->id;

		 $valRow->save();

		 $this->view->valRow = $valRow;

		 if ($valRow->getCost() > 0)
		   $this->view->valuation = "&pound;".number_format($valRow->getCost(), 2);
		 else
		   $this->view->pleaseContact = true;

		 $this->_helper->viewRenderer->setRender('tpl1-step8'); 
	   }

	   if ($_POST['step'] == 9)
	   {
		 $dataCombined = unserialize(base64_decode($_POST['prev_data']));
	     
		 $valuationsTable = new Application_Model_CarValuations();

		 $valRow = $valuationsTable->fetchRow($valuationsTable->select()->where('id = ?', $_POST['valRowId'] ));
		 if ($valRow->reg == $dataCombined['reg'])
		 {
           list($month, $day, $year) = explode("/", $_POST['ap_date']);

		   $valRow->ap_date = $year."-".$month."-".$day;
		   $valRow->save();
		 }

		 $this->_helper->viewRenderer->setRender('tpl1-step9'); 
	   }
	}
  }
